//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.lyq.bean.mySql;

import com.lyq.bean.Course;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

public class SeekC_Sql {
    public SeekC_Sql() {
    }

    public List<Course> seekC_Sql(String select, String seek, String name, List<Course> list) {
        (new MysqlLinking()).getLink();
        Connection conn = MysqlLinking.conn;
        if (conn != null) {
            try {
                String sql = "";
                PreparedStatement ps = null;
                
                // 处理按类别搜索的特殊情况
                if (select.equals("分类")) {
                    sql = "select * from course where category=?";
                    ps = conn.prepareStatement(sql);
                    ps.setString(1, name);
                } else {
                    if (select.equals("编号")) {
                        select = "C_No";
                    } else if (select.equals("名称")) {
                        select = "C_Name";
                    } else if (select.equals("授课教师")) {
                        select = "C_Teacher_ID";
                    }

                    if (seek.equals("模糊查询")) {
                        sql = "select * from course where " + select + " like '%" + name + "%'";
                        ps = conn.prepareStatement(sql);
                    } else if (seek.equals("精确查询")) {
                        sql = "select * from course where " + select + "=?";
                        ps = conn.prepareStatement(sql);
                        ps.setString(1, name);
                    }
                }

                ResultSet rs = ps.executeQuery();

                while(rs.next()) {
                    Course c = new Course();
                    c.setNo(rs.getString("C_No"));
                    c.setName(rs.getString("C_Name"));
                    c.setTime(rs.getString("C_Time"));
                    c.setPlace(rs.getString("C_Place"));
                    c.setCredit(rs.getString("C_Credit"));
                    c.setTeacher(rs.getString("C_Teacher_ID"));
                    c.setLimitNum(rs.getString("C_LimitNum"));
                    c.setResidueNum(rs.getString("C_ResidueNum"));
                    c.setCourseType(rs.getString("course_type"));
                    c.setCampus(rs.getString("campus"));
                    c.setCategory(rs.getString("category"));
                    list.add(c);
                }
                
                rs.close();
                ps.close();
            } catch (Exception var10) {
                Exception e = var10;
                e.printStackTrace();
            }
        } else {
            System.out.println("数据库连接错误！");
        }

        return list;
    }



    // 在 ElectC_Sql 类中添加以下方法
    public Course getCourseById(String courseId) {
        (new MysqlLinking()).getLink();
        Connection conn = MysqlLinking.conn;
        Course course = null;

        if (conn != null) {
            try {
                String sql = "SELECT * FROM course WHERE C_No = ?";
                PreparedStatement ps = conn.prepareStatement(sql);
                ps.setString(1, courseId);

                ResultSet rs = ps.executeQuery();
                if (rs.next()) {
                    course = new Course();
                    course.setNo(rs.getString("C_No"));
                    course.setName(rs.getString("C_Name"));
                    // 根据 Course 类的实际字段设置其他属性
                    // 例如：
                    // course.setCredit(rs.getInt("Credit"));
                    // course.setTeacher(rs.getString("Teacher"));
                    // course.setPlace(rs.getString("Place"));
                    // course.setTime(rs.getString("Time"));
                    course.setMajorId(rs.getInt("major_id"));
                    course.setYearId(rs.getInt("year_id"));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("数据库连接错误！");
        }

        return course;
    }
}